package cn.onein.edu.web.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.onein.edu.biz.bean.User;
import cn.onein.edu.web.dao.LoginDao;
import cn.onein.edu.web.service.LoginService;

/**
 * 用户登录service
 * @author Upton
 *
 */
@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	private LoginDao loginDao;
	
	@Override
	public User login(String userName, String password, String lastIP) {
		if(userName == null || userName.trim().length() <= 0 || 
		   password == null || password.length() <= 0 ||
		   lastIP == null || lastIP.length() <= 0){
			return null;
		}
		User user = loginDao.queryUser(userName, password);
		if(user != null){
			loginDao.updateUserLoginInfo(user.getId(), lastIP);
		}
		
		return user;
	}

	@Override
	public User getUserByName(String userName) {
		
		return loginDao.queryUserByName(userName);
	}

	@Override
	public void updateUserPassword(String password, Long id) {
		Map<String,Object> param = new HashMap<String,Object>();
		
		param.put("id", id);
		param.put("password", password);
		
		loginDao.updateUserPassword(param);
	}
}
